package com.tywj.lib.social

import com.tywj.lib.social.entity.AlipayPayOrderEntity
import com.tywj.lib.social.entity.PayOrderEntity
import com.tywj.lib.social.pay.AlipayPay
import com.tywj.lib.social.pay.WeChatPay
import com.tywj.lib.social.share.WeChatShare
import com.alipay.share.sdk.openapi.APAPIFactory
import com.alipay.share.sdk.openapi.IAPApi
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tywj.lib.social.interfaces.*
import com.tywj.lib.core.common.util.AppUtil
import com.tywj.lib.social.login.WeChatLogin
import com.tywj.lib.social.share.AlipayShare

/**
 * <pre>
 *     author : June Yang
 *     time   : 2019/02/19
 *     desc   : 社会化工具类
 *     version: 1.0.0
 * </pre>
 */
object SocialUtil {

    // 微信appId
    var weChatAppId = ""

    // 支付宝AppId
    var alipayAppId = ""

    // 支付
    val pay by lazy { SocialPay() }

    // 分享
    val share by lazy { SocialShare() }

    // 登录
    val login by lazy { SocialLogin() }

    // 微信Api
    internal val wxApi: IWXAPI by lazy {
        if (weChatAppId == "") {
            throw IllegalArgumentException("you must set WeChat AppId before use")
        }
        val api = WXAPIFactory.createWXAPI(
            AppUtil.context,
            weChatAppId, true
        )
        api.registerApp(weChatAppId)
        return@lazy api
    }

    // 支付宝api
    internal val alipayApi: IAPApi by lazy {
        if (alipayAppId == "") {
            throw IllegalArgumentException("you must set Alipay AppId before use")
        }
        APAPIFactory.createZFBApi(
            AppUtil.context,
            alipayAppId, true
        )
    }

    class SocialPay {
        // 支付结果回调
        internal var callback: Callback? = null

        // 微信支付
        val weChat: IPay<PayOrderEntity> by lazy { WeChatPay() }

        // 支付宝支付
        val alipay: IPay<AlipayPayOrderEntity> by lazy { AlipayPay() }
    }

    class SocialShare {
        // 分享结果回调
        internal var callback: Callback? = null

        // 微信分享
        val weChat: IShare by lazy { WeChatShare() }

        // 支付宝分享
        val alipay: IShare by lazy { AlipayShare() }
    }

    // 微信登录
    class SocialLogin {
        // 登录结果回调
        internal var weChatLoginCallBack: IWeChatLoginCallBack? = null

        //微信登录
        val weChatLogin: ILogin by lazy { WeChatLogin() }
    }
}